package top.melody.ecommerce.platform.service.account.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import top.melody.ecommerce.common.util.page.PageRows;
import top.melody.ecommerce.common.util.page.Pagination;
import top.melody.ecommerce.common.util.security.DES3;
import top.melody.ecommerce.platform.EcommerceException;
import top.melody.ecommerce.platform.api.domain.account.Account;
import top.melody.ecommerce.platform.api.service.AccountService;
import top.melody.ecommerce.platform.dal.account.AccountMapper;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

    private static final Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class);
    @Resource
    private AccountMapper accountMapper;

    @Override
    public Account signUp(Account account) throws EcommerceException {
        try {
            account.setAccountPassword(DES3.encode(account.getAccountPassword()));
            accountMapper.insert(account);
            return account;
        } catch (Exception e) {
            throw new EcommerceException("create account error...", e);
        }
    }

    @Override
    public Account getAccount(int accountId) throws EcommerceException {
        try {
            return accountMapper.get(accountId);
        } catch (Exception e) {
            throw new EcommerceException("get account by accountId error...", e);
        }
    }

    @Override
    public Account getAccount(String userId) throws EcommerceException {
        try {
            return accountMapper.getByUserId(userId);
        } catch (Exception e) {
            throw new EcommerceException("get account by userId error...", e);
        }
    }

    @Override
    public Account getAccountByMobile(String mobile) throws EcommerceException {
        try {
            return accountMapper.getByMobile(mobile);
        } catch (Exception e) {
            throw new EcommerceException("get account by accountId error...", e);
        }
    }

    @Override
    public Account updateAccount(Account account) throws EcommerceException {
        try {
            accountMapper.updateByPrimaryKey(account);
            return getAccount(account.getAccountId());
        } catch (Exception e) {
            throw new EcommerceException("execute mapper update account exception", e);
        }
    }

    @Override
    public PageRows<Account> queryByPage(Account queryEntity, Pagination page) throws EcommerceException {
        PageRows<Account> pageRows = new PageRows<Account>();
        try {
            List<Account> userList = accountMapper.queryByPage(queryEntity, page.getStartRowIdx(),page.getPageSize());
            int count = accountMapper.getCount(queryEntity);
            if (page == null)
                page = new Pagination(count, 1, count);
            else
                page.setTotalRows(count);
            pageRows.setPage(page);
            pageRows.setRows(userList);
            return pageRows;
        } catch (Exception e) {
            throw new EcommerceException("execute mapper query exception", e);
        }
    }

    @Override
    public Account getAccountUser(Integer accountId) throws EcommerceException {
        try {
            return accountMapper.get(accountId);
        } catch (Exception e) {
            throw new EcommerceException("execute mapper get account exception", e);
        }
    }


}
